package com.example.dell.fastshopping.user;

import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.jiguang.PushMessage;
import com.example.dell.fastshopping.user.modle.MessageActivityModle;
import com.example.dell.fastshopping.utils.DateUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MessageActivity extends AppCompatActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.right_text)
    TextView rightText;
    @BindView(R.id.nodata)
    LinearLayout nodata;
    private MessageActivityModle modle;
    private List<PushMessage> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setData();
    }

    private void initView() {
        title.setText("消息");
        modle = ViewModelProviders.of(this).get(MessageActivityModle.class);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }

    private void setData() {
        list = modle.getList();
        modle.clearNum();
        if (list == null || list.size() == 0) {
            nodata.setVisibility(View.VISIBLE);
        } else {
            nodata.setVisibility(View.GONE);
            rv.setAdapter(new MyAdapter());
        }
    }

    @OnClick(R.id.back)
    public void onClick() {
        finish();
    }

    private class MyAdapter extends RecyclerView.Adapter<MyHolder> {
        @NonNull
        @Override
        public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View inflate = LayoutInflater.from(MessageActivity.this).inflate(R.layout.item_massage, viewGroup, false);
            MyHolder myHolder = new MyHolder(inflate);
            return myHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
            PushMessage pushMessage = list.get(i);
            myHolder.title.setText(pushMessage.getTitle());
            myHolder.content.setText(pushMessage.getContent());
            myHolder.time.setText(DateUtils.stampToDate(pushMessage.getTime()));
        }


        @Override
        public int getItemCount() {
            return list.size();
        }
    }

    private class MyHolder extends RecyclerView.ViewHolder {

        public TextView title;
        public TextView content;
        public TextView time;

        public MyHolder(View inflate) {
            super(inflate);
            title = inflate.findViewById(R.id.title);
            content = inflate.findViewById(R.id.content);
            time = inflate.findViewById(R.id.time);


        }

    }
}
